package com.tz.actions.cart;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.tz.entity.Cart;
import com.tz.entity.OrderItem;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.Result;

import java.util.HashSet;
import java.util.Map;
import java.util.Set;


/**
 * Created by heziqian on 2016/10/21.
 */
@Namespace("/")
public class ClearAction extends ActionSupport{

    @Action(value = "clear",
            results = {@Result(location="/cart.jsp"),
                    @Result(name = "input",location = "/cart.jsp")
            })
    public String execute() {

        ActionContext context=ActionContext.getContext();
        Map<String,Object> session=context.getSession();
        Cart cart=(Cart)session.get("cart");

        Set<OrderItem> items=new HashSet<>();
        cart.setItems(items);
        cart.setTotalPrice(0);
        cart.setCount(0);
        session.put("cart",cart);

        return SUCCESS;

    }
}
